package com.jiyun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.client.WangpanFileClient;
import com.jiyun.dto.ShareInfoDto;
import com.jiyun.entity.ShareInfo;
import com.jiyun.entity.WangpanFile;
import com.jiyun.pojo.MyResult;
import com.jiyun.service.IShareInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  控制层
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-04-23
 */
@RestController
@RequestMapping("/share-info")
public class ShareInfoController {

    @Autowired
    IShareInfoService shareInfoService;
    @RequestMapping("validCode")
    public MyResult validCode(Integer sid,String code){
        ShareInfo shareInfo = shareInfoService.getById(sid);

        if (!shareInfo.getCode().equals(code)) {
            return MyResult.ERROR("提取码有误");
        }

        return MyResult.OK(shareInfo);
    }


    @RequestMapping("validInfo")
    public MyResult validInfo(Integer sid){
        ShareInfo shareInfo = shareInfoService.getById(sid);
        if (shareInfo==null) {
            return MyResult.ERROR("链接不存在");
        }
        if (shareInfo.getState().equals("已取消")) {
            return MyResult.ERROR("链接已取消分享");
        }
        if (shareInfo.getExpireTime().isBefore(LocalDateTime.now())) {
            return MyResult.ERROR("链接已过期");
        }
        return MyResult.OK();
    }


    @RequestMapping("shareFile")
    public MyResult shareFile(@RequestBody ShareInfoDto shareInfoDto){
        /**
         * 1、校验总的分享数量
         * 2、已经分享过提示
         */

        LambdaQueryWrapper<ShareInfo> shareInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shareInfoLambdaQueryWrapper.eq(ShareInfo::getUid,shareInfoDto.getUid());
        if (shareInfoService.count(shareInfoLambdaQueryWrapper)==10) {
            return MyResult.ERROR("你的分享数量已用完");
        }
        shareInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shareInfoLambdaQueryWrapper.eq(ShareInfo::getUid,shareInfoDto.getUid());
        shareInfoLambdaQueryWrapper.eq(ShareInfo::getFid,shareInfoDto.getFid());
        if (shareInfoService.count(shareInfoLambdaQueryWrapper)>0) {
            return MyResult.ERROR("这个文件已经分享过啦");
        }

        /**
         *
         */

        ShareInfo shareInfo = new ShareInfo();
        BeanUtils.copyProperties(shareInfoDto,shareInfo);
        shareInfo.setShareTime(LocalDateTime.now());
        shareInfo.setExpireTime(LocalDateTime.now().plusDays(shareInfoDto.getDays()));
        shareInfoService.save(shareInfo);


        return MyResult.OK(shareInfo.getId());



    }

    /**
     * 添加接口
     */
    @RequestMapping("/add")
    public void addShareInfo(@RequestBody ShareInfo shareInfo){
        shareInfoService.save(shareInfo);
    }

    /**
     * 修改接口
     */
    @RequestMapping("/update")
    public void updateShareInfo(@RequestBody ShareInfo shareInfo){
        shareInfoService.updateById(shareInfo);
    }

    /**
     * 单个删除接口
     */
    @RequestMapping("/deleteOne")
    public void deleteOne(@RequestParam Integer id){
        shareInfoService.removeById(id);
    }

    @RequestMapping("/cancleShare")
    public void cancleShare(@RequestParam Integer id){
        ShareInfo shareInfo = new ShareInfo();
        shareInfo.setId(id);
        shareInfo.setState("已取消");
        shareInfoService.updateById(shareInfo);
    }




    /**
     * 批量删除接口
     * 前台ajax参数ids传入示例：1,2,3  (英文逗号分隔的字符串)
     */
    @RequestMapping("/deleteBatch")
    public void deleteBatch(@RequestParam List<Integer> ids){
        shareInfoService.removeByIds(ids);
    }

    /**
     * 根据id查询实体接口
     */
    @RequestMapping("/findById")
    public ShareInfo findById(@RequestParam Integer id){
        return shareInfoService.getById(id);
    }


    @Autowired
    WangpanFileClient wangpanFileClient;
    /**
     * 查询所有接口
     */
    @RequestMapping("/list")
    public List<ShareInfoDto> findAll(Integer uid){
        LambdaQueryWrapper<ShareInfo> shareInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shareInfoLambdaQueryWrapper.eq(ShareInfo::getUid,uid);
        List<ShareInfo> list = shareInfoService.list(shareInfoLambdaQueryWrapper);


        ArrayList<ShareInfoDto> shareInfoDtos = new ArrayList<>();

        if(CollectionUtils.isNotEmpty(list)){
            for (ShareInfo shareInfo : list) {
                ShareInfoDto shareInfoDto = new ShareInfoDto();

                BeanUtils.copyProperties(shareInfo,shareInfoDto);
                WangpanFile wangpanFile = wangpanFileClient.findById(shareInfo.getFid());
                shareInfoDto.setWangpanFile(wangpanFile);
                shareInfoDtos.add(shareInfoDto);

            }
        }



        return shareInfoDtos;
    }





    /**
     * 分页查询接口
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     */
    @RequestMapping("/findPage")
    public Page<ShareInfo> findPage(@RequestParam Integer page,@RequestParam Integer pageSize){
        Page<ShareInfo> shareInfoPage = new Page<>(page, pageSize);
        return shareInfoService.page(shareInfoPage);
    }

}
